<?php
namespace Communal\Controller;
use Common\Controller\CommonController;

/**
 * Talk模块
 */
class TalkController extends CommonController {
	
	protected function _initialize(){
		parent::_initialize();
	}
	
	
	
	/**
	 * 聊天服务器数据初始化接口,用于chatserver调用
	 */
	public function talkInitAction(){
		$this->_talkInit();
	}
	
	private function _talkInit(){
		$type = (int)I('request.type');
		//获取某个用户的好友数据
		if($type == 1){
			$custid = (int)I('request.id');
			$returnData['type'] = $type;
			$returnData['cmd'] = 0;
			$returnData['data'] = array();
			
			if(!empty($custid)){
				$data = array();
				$friendList = $this->_getFriendList($custid);
				if(!empty($friendList)){
					foreach($friendList as $k=>&$v){
						array_push($data, (int)$v['custid']);
					}
				}
				
				$returnData['data'][0]['id'] = $custid;
				$returnData['data'][0]['list'] = $data;
			}else{
				if(empty($this->_friend)) $this->_friend = new \Common\Model\FriendModel();
				$custids = $this->_friend->getDistinctCustId();
				if(!empty($custids)){
					foreach($custids as $idx=>&$val){
						$data = array();
						$custid = $val['custid'];
						$friendList = $this->_getFriendList($custid);
						if(!empty($friendList)){
							foreach($friendList as $k=>&$v){
								array_push($data, (int)$v['custid']);
							}
						}
						
						$returnData['data'][$idx]['id'] = (int)$custid;
						$returnData['data'][$idx]['list'] = $data;
					}
				}
			}
	
			$this->ajaxReturn($returnData);
		}
		 
		 
		//获取某个群聊的用户数据
		if($type == 2){
			$chatid = (int)I('request.id');
			$returnData['type'] = $type;
			$returnData['cmd'] = 0;
			$returnData['data'] = array();
			
			$data = array();
			$custList = $this->_getChatCust($chatid);
			if(!empty($custList)){
				foreach($custList as $k=>&$v){
					array_push($data, (int)$v['custid']);
				}
			}
			
			$returnData['data'][0]['id'] = $chatid;
			$returnData['data'][0]['list'] = $data;
			
	
			$this->ajaxReturn($returnData);
		}
		 
		 
		//获取某个用户的黑名单数据
		if($type == 3){
			$custid = (int)I('request.id');
			$returnData['type'] = $type;
			$returnData['cmd'] = 0;
			$returnData['data'] = array();
			$this->_cust_blacklist = new \Common\Model\CustBlackListModel();
			$custids = $this->_cust_blacklist->getDistinctCustId($custid);
			if(!empty($custids)){
				foreach($custids as $idx=>&$val){
					$data = array();
					$list = $this->_cust_blacklist->getBlacklistIdsByCustId($val['custid']);
					if(!empty($list)){
						foreach($list as $k=>&$v){
							array_push($data, (int)$v['tustid']);
						}
					}
					$returnData['data'][$idx]['id'] = $val['custid'];
					$returnData['data'][$idx]['list'] = $data;
				}
			}
	
			$this->ajaxReturn($returnData);
		}
		 
		 
		//获取某个用户所有群聊ID数据
		if($type == 4){
			$custid = (int)I('request.id');
			$returnData['type'] = $type;
			$returnData['cmd'] = 0;
			$returnData['data'] = array();
			
			
			$data = array();
			$chatList = $this->_getChatList($custid);
			if(!empty($chatList)){
				foreach($chatList as $idx=>&$val){
					array_push($data, (int)$val['chatid']);
				}
			}
			
			$returnData['data'][0]['id'] = (int)$custid;
			$returnData['data'][0]['list'] = $data;
	
			$this->ajaxReturn($returnData);
		}
		 
		 
		//获取系统账户数据
		if($type == 5){
			$custid = (int)I('request.id');
			$returnData['type'] = $type;
			$returnData['cmd'] = 0;
			$returnData['data'] = array();
	
			$returnData['data'][0]['id'] = (int)$custid;
			$returnData['data'][0]['list'] = array(1,2,3,4,5,6,7,8,9);
	
			$this->ajaxReturn($returnData);
		}
	}
    
}
